<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
 */
class item_db extends Model
{
	public $pk = 'item_id';
	public $table = 'ptx_item';
	var $linker = array(
			'share'=>array(
				'type' => 'hasmany',  
				'map' => 'share',  
				'mapkey' => 'item_id', 
				'ftable' => 'ptx_share',
				'fkey' => 'item_id',
				'enabled' => false
			),
			'itemdetail'=>array(
					'type' => 'hasone',
					'map' => 'itemdetail',
					'mapkey' => 'item_id',
					'ftable' => 'ptx_item_detail',
					'fkey' => 'item_id',
					'enabled' => false
			),
	);

	private function init_conditions($conditions){
		$conditions_item = ' ptx_item.is_deleted=0 ';
		if(isset($conditions['keyword'])){
			$conditions_item .= 'AND MATCH (ptx_item.intro_search) AGAINST ("'.$conditions['keyword'].'" IN BOOLEAN MODE) ';
		}

		if(isset($conditions['is_show'])){
			$conditions_item .= 'AND ptx_item.is_show='.$conditions['is_show'].' ';
		}
		//查找所有商品
		if(isset($conditions['shopping'])){
			$conditions_item .= 'AND ptx_item.price>0 ';
		}

		if(strpos($conditions_item, 'AND') === 0){
			$conditions_item = substr($conditions_item, 3);
		}
		return $conditions_item;
	}

	public function search($conditions=NULL,$page,$pagesize,$fields = null,$sort=null){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_item.item_id DESC ";
		return $this->pager($page, $pagesize)->findAllJoin($conditions,$sort,$fields);
	}

	function addone($data){
		$attrs = array('user_id','user_nickname','title','keyword','keyword_search','color'
						,'image_path_0','image_path_1','image_path_2','width','height','item_type'
						,'share_type','price','is_show','total_images','summary');
		$insertData = array();
		foreach ($attrs as $attr){
			if($data[$attr]) $insertData[$attr]=$data[$attr];
		}
		$insertData['create_time']=TIMESTAMP;
		return $this->create($insertData);
	}

	function updateone($itemid,$data){
		$attrs = array('user_id','user_nickname','title','keyword','keyword_search','color'
						,'image_path_0','image_path_1','image_path_2','width','height','item_type'
						,'share_type','price','is_show','total_images','summary');
		$insertData = array();
		foreach ($attrs as $attr){
			if($data[$attr]) $insertData[$attr]=$data[$attr];
		}
		return $this->update(array('item_id'=>$itemid),$insertData);
	}
	
}
